/*
 * RateMonotonic.cpp
 *
 *  Created on: Dec 19, 2011
 *      Author: quotrader
 */

#include "RM.h"

RM::RM() {
	this->name = "Rate-Monotonic";
}



void RM::run(TaskMng *tMng){
	Task *taskList=tMng->tasks;
	int nof= tMng->numTask;
	Task tmpTL[nof];
	int minPeriod=0;
	int maxPeriod=0;
	int minIndex=0;

	// copy task list to temporary place and get biggest period
	for(int i=0; i< nof; i++){
		tmpTL[i]=taskList[i];
		if(maxPeriod < tmpTL[i].period) maxPeriod= tmpTL[i].period;
	}


	// order tasks and put them back to their original place
	for(int i=0; i< nof; i++){
		minPeriod= maxPeriod;
		for(int j=0; j<nof; j++)
			if(minPeriod >= tmpTL[j].period && tmpTL[j].period != -1){
				minPeriod=tmpTL[j].period;
				minIndex= j;
			}
		if(tmpTL[minIndex].period != -1)taskList[i]=tmpTL[minIndex];
		tmpTL[minIndex].period=-1;
	}
	tMng->changed=false;
}

int RM::rePrioritize(TaskMng *tMng,int sp, int ip){return 0;}
